package com.daigoudao.web.action;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.daigoudao.web.common.BasePageAction;
import com.daigoudao.web.model.AreaModel;
import com.daigoudao.web.model.CanBuyModel;
import com.daigoudao.web.model.CategoryModel;
import com.daigoudao.web.model.FollowModel;
import com.daigoudao.web.model.UserModel;
import com.daigoudao.web.model.WantBuyModel;
import com.daigoudao.web.service.CacheService;
import com.daigoudao.web.service.ICanBuyService;
import com.daigoudao.web.service.ICommonService;
import com.daigoudao.web.service.IUserService;
import com.daigoudao.web.service.IWantBuyService;

public class HomepageAction extends BasePageAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5150006703156379466L;

	private static Logger logger = LoggerFactory
			.getLogger(HomepageAction.class);

	private UserModel userModel;
	private List<CanBuyModel> canBuyList;

	private List<WantBuyModel> wantBuyList;
	private List<UserModel> brokerList;
	private List<UserModel> hotBrokerList;
	private List<AreaModel> areaList;
	private List<CategoryModel> cateList;

	private ICanBuyService canBuyService;
	private IWantBuyService wantBuyService;
	private IUserService userService;
	private ICommonService commonService;

	private CacheService cacheService;

	public CacheService getCacheService() {
		return cacheService;
	}

	public void setCacheService(CacheService cacheService) {
		this.cacheService = cacheService;
	}

	public UserModel getUserModel() {
		return userModel;
	}

	public void setUserModel(UserModel userModel) {
		this.userModel = userModel;
	}

	public List<CanBuyModel> getCanBuyList() {
		return canBuyList;
	}

	public void setCanBuyList(List<CanBuyModel> canBuyList) {
		this.canBuyList = canBuyList;
	}

	public List<WantBuyModel> getWantBuyList() {
		return wantBuyList;
	}

	public void setWantBuyList(List<WantBuyModel> wantBuyList) {
		this.wantBuyList = wantBuyList;
	}

	public List<UserModel> getBrokerList() {
		return brokerList;
	}

	public void setBrokerList(List<UserModel> brokerList) {
		this.brokerList = brokerList;
	}

	public List<UserModel> getHotBrokerList() {
		return hotBrokerList;
	}

	public void setHotBrokerList(List<UserModel> hotBrokerList) {
		this.hotBrokerList = hotBrokerList;
	}

	public List<AreaModel> getAreaList() {
		return areaList;
	}

	public void setAreaList(List<AreaModel> areaList) {
		this.areaList = areaList;
	}

	public List<CategoryModel> getCateList() {
		return cateList;
	}

	public void setCateList(List<CategoryModel> cateList) {
		this.cateList = cateList;
	}

	public ICanBuyService getCanBuyService() {
		return canBuyService;
	}

	public void setCanBuyService(ICanBuyService canBuyService) {
		this.canBuyService = canBuyService;
	}

	public IWantBuyService getWantBuyService() {
		return wantBuyService;
	}

	public void setWantBuyService(IWantBuyService wantBuyService) {
		this.wantBuyService = wantBuyService;
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public ICommonService getCommonService() {
		return commonService;
	}

	public void setCommonService(ICommonService commonService) {
		this.commonService = commonService;
	}

	public String initHomepage() {

		try {
			UserModel user = super.getUserLogin();
			int userId = 0;
			/*
			 * if (user == null) { hotBrokerList =
			 * cacheService.getHotBrokerList(); } else { int userId =
			 * user.getUserid(); userModel = new UserModel();
			 * userModel.setUserid(userId); userModel.setPageSize(6);
			 * 
			 * hotBrokerList = userService.getHotBrokerList(userModel); }
			 */

			if (user != null) {
				userId = user.getUserid();
			}
			userModel = new UserModel();
			userModel.setUserid(userId);
			userModel.setPageSize(7);

			hotBrokerList = userService.getHotBrokerList(userModel);

			canBuyList = cacheService.getHotCanBuyList();
			wantBuyList = cacheService.getNewWantBuyList();
			areaList = cacheService.getAreaList();
			cateList = cacheService.getCateList();

		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("initHomepage error:", e);
			}
			return ERROR;
		}
		return SUCCESS;
	}

	public void followUser() {
		String result = "false";

		FollowModel follow = new FollowModel();
		String flag = request.getParameter("flag");
		String follower = request.getParameter("userId");

		UserModel user = super.getUserLogin();
		int userId = user.getUserid();

		follow.setFollower(Integer.parseInt(follower));
		follow.setUserId(userId);

		try {

			if ("1".equals(flag)) {
				result = "" + userService.saveFollower(follow);
			} else if ("2".equals(flag)) {
				result = "" + userService.delFollower(follow);
			} else if ("3".equals(flag)) {
				result = this.getHotFollowerToStr(follow);
			} else if ("4".equals(flag)) {
				result = this.getHotFollowerToStr(follow);
				result += "#" + this.getFollowerToStr(follow);
			} else {
				result = "" + userService.getFollowCountByUserId(follow);
				if ("false".equals(result)) {
					result = "" + userService.saveFollower(follow);
				}
			}

			response.getWriter().print(result);

		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("followUser error:", e);
			}
		}
	}

	public String getBrokers() {

		try {
			cateList = commonService.queryCategorys();
			cateList.add(0, new CategoryModel(0, "不限"));
			areaList = commonService.queryAreas();
			areaList.add(0, new AreaModel(0, "不限"));

			UserModel user = super.getUserLogin();
			int userId = 0;

			if (user != null) {
				userId = user.getUserid();
			}
			UserModel query = new UserModel();
			query.setNickName(getQ());
			if (getO() != 0) {
				query.setOrder(getO());
			} else {
				query.setOrder(2);
			}
			query.setAreaId(getArea());
			query.setCategoryId(getCategory());
			query.setStartIndex((getSuperPage() - 1) * getPageSize());
			query.setUserid(userId);
			query.setPageSize(getPageSize());
			brokerList = userService.getBrokerList(query);

			hotBrokerList = userService.getHotBrokerList(query);
			setTotalSize(userService.getBrokerCount(query));
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getBrokers error:", e);
			}
			return ERROR;
		}
		return SUCCESS;
	}

	private String getHotFollowerToStr(FollowModel follow) {
		String str = "";
		boolean flag = false;
		int follower = follow.getFollower();
		userModel = new UserModel();
		userModel.setUserid(follow.getUserId());
		userModel.setPageSize(7);

		try {
			hotBrokerList = userService.getHotBrokerLista(userModel);

			if (hotBrokerList != null && !hotBrokerList.isEmpty()) {
				for (UserModel broker : hotBrokerList) {
					int followId = broker.getFollowId();
					if (followId != 0) {
						int uFollower = broker.getUserid();
						if (follower == uFollower) {
							flag = true;
						}
						if (str.equals("")) {
							str += uFollower;
						} else {
							str += "|" + uFollower;
						}
					}
				}
			}

			if (!flag) {
				boolean _flag = userService.saveFollower(follow);
				if (_flag) {
					if (str.equals("")) {
						str += follower;
					} else {
						str += "|" + follower;
					}
				}
			}
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getHotFollowerToStr error:", e);
			}
			return ERROR;
		}
		return str.toString();
	}

	private String getFollowerToStr(FollowModel follow) {
		String str = "";
		boolean flag = false;
		int follower = follow.getFollower();
		userModel = new UserModel();
		userModel.setUserid(follow.getUserId());
		userModel.setStartIndex((getSuperPage() - 1) * getPageSize());
		userModel.setPageSize(getPageSize());

		try {
			brokerList = userService.getBrokerLista(userModel);

			if (brokerList != null && !brokerList.isEmpty()) {
				for (UserModel broker : brokerList) {
					int followId = broker.getFollowId();
					if (followId != 0) {
						int uFollower = broker.getUserid();
						if (follower == uFollower) {
							flag = true;
						}
						if (str.equals("")) {
							str += uFollower;
						} else {
							str += "|" + uFollower;
						}
					}
				}
			}

			if (!flag) {
				boolean _flag = userService.saveFollower(follow);
				if (_flag) {
					if (str.equals("")) {
						str += follower;
					} else {
						str += "|" + follower;
					}
				}
			}
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getFollowerToStr error:", e);
			}
			return ERROR;
		}
		return str.toString();
	}

}
